package com.msr.controller;

import com.msr.bean.User;
import com.msr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author tom
 * @version V1.0
 * @Package com.msr.controller
 * @date 2024/1/9 10:31
 * @Copyright © 株式会社多言语系统研究所
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 跳转到登录页面
     * @return
     */
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    /**
     * 退出
     * @return
     */
    @RequestMapping("/lgout")
    public String lgout(HttpSession session){
        //清空session数据
        session.invalidate();
        return "login";
    }


    /**
     * 处理登录的业务代码
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/user_login")
    public String login(String username, String password, HttpSession session){
        System.out.println("=======================");
        User user = userService.login(username,password);
        if(user!=null){
            //登录成功:跳转到index页面
            //保存用户的数据
            session.setAttribute("user",user);
            return "index";
        }else{
            //登录失败：回到登录页面
            return "login";
        }
    }
}
